<?php

namespace Swof\Config;

use ArrayAccess;

class Repository implements ArrayAccess
{
    /**
     * All of the configuration items.
     *
     * @var string
     */
    protected $configPath;

    /**
     * All of the configuration items.
     *
     * @var array
     */
    protected $items = [];

    /**
     * Create a new configuration repository.
     *
     * @param  array  $items
     * @return void
     */
    public function __construct()
    {
        $this->configPath = app()->basePath() . '/config';

        $this->items = $this->parseConfigs();
    }

    /**
     * Note: 遍历文件，返回数组
     *
     * @return array
     * @author: Zhao Zhiqiang
     * @since: 2021/5/26 16:40
     */
    protected function parseConfigs(): array
    {
        $files = scandir($this->configPath);
        $data = null;

        foreach ($files as $file) {
            if ($file === '.' || $file === '..') {
                continue;
            }

            $filename = \stristr($file, ".php", true);

            $data[$filename] = require_once $this->configPath. '/' .$file;
        }

        return $data;
    }

    /**
     * Determine if the given configuration value exists.
     *
     * @param  string  $key
     * @return bool
     */
    public function has(string $key): bool
    {
        $subKeyArray = $this->items;

        foreach (explode('.', $key) as $segment) {
            if (isset($subKeyArray[$segment])) {
                $subKeyArray = $subKeyArray[$segment];
            } else {
                return false;
            }
        }

        return true;
    }

    /**
     * Get the specified configuration value.
     *
     * @param  array|string  $key
     * @param  mixed  $default
     * @return mixed
     */
    public function get($key, $default = null)
    {
        if (is_array($key)) {
            return $this->getMany($key);
        }

        if (array_key_exists($key, $this->items)) {
            return $this->items[$key];
        }

        if (strpos($key, '.') === false) {
            return $this->items[$key] ?? value($default);
        }

        $array = $this->items;

        foreach (explode('.', $key) as $segment) {
            if (isset($array[$segment])) {
                $array = $array[$segment];
            } else {
                return value($default);
            }
        }

        return $array;
    }

    /**
     * Get many configuration values.
     *
     * @param array $keys
     * @return array
     */
    public function getMany(array $keys): array
    {
        $config = [];

        foreach ($keys as $key => $default) {
            if (is_numeric($key)) {
                [$key, $default] = [$default, null];
            }

            $config[$key] = $this->get($key, $default);
        }

        return $config;
    }

    /**
     * Set a given configuration value.
     *
     * @param  array|string  $key
     * @param  mixed  $value
     * @return array
     */
    public function set($key, $value = null): array
    {
        $keys = is_array($key) ? $key : [$key => $value];

        $array = $this->items;

        foreach ($keys as $key => $value) {
            if (is_null($key)) {
                return $value;
            }

            $tmpKeys = explode($key, '.');

            foreach ($tmpKeys as $i => $k) {
                if (count($k) === 1) {
                    break;
                }

                unset($keys[$i]);

                if (! isset($array[$key]) || ! is_array($array[$key])) {
                    $array[$key] = [];
                }

                $array = &$array[$key];
            }

            $array[end($keys)] = $value;
        }

        return $array;
    }

    /**
     * Prepend a value onto an array configuration value.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return void
     */
    public function prepend($key, $value)
    {
        $array = $this->get($key);

        array_unshift($array, $value);

        $this->set($key, $array);
    }

    /**
     * Push a value onto an array configuration value.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return void
     */
    public function push(string $key, $value)
    {
        $array = $this->get($key);

        $array[] = $value;

        $this->set($key, $array);
    }

    /**
     * Get all of the configuration items for the application.
     *
     * @return array
     */
    public function all()
    {
        return $this->items;
    }

    /**
     * Determine if the given configuration option exists.
     *
     * @param  string  $key
     * @return bool
     */
    public function offsetExists($key)
    {
        return $this->has($key);
    }

    /**
     * Get a configuration option.
     *
     * @param  string  $key
     * @return mixed
     */
    public function offsetGet($key)
    {
        return $this->get($key);
    }

    /**
     * Set a configuration option.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return void
     */
    public function offsetSet($key, $value)
    {
        $this->set($key, $value);
    }

    /**
     * Unset a configuration option.
     *
     * @param  string  $key
     * @return void
     */
    public function offsetUnset($key)
    {
        $this->set($key, null);
    }
}
